/**
 * Created by zdd on 2020/11/6.
 */

// 时间转换
function parseTime(time, cFormat){
    if (arguments.length === 0) {
        return null
    } else if (time === undefined || time === null) {
        return ''
    }
    const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
    let date
    if (typeof time === 'object') {
        date = time
    } else {
        if (('' + time).length === 10) time = parseInt(time) * 1000
        date = new Date(+time)
    }
    const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay(),
    }
    const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        let value = formatObj[key]
        // Note: getDay() returns 0 on Sunday
        if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
        if (result.length > 0 && value < 10) {
            value = '0' + value
        }
        return value || 0
    })
    return timeStr
}
// 将字符串转化成标注日期格式
function formatDate(string, cFormat) {
    let format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
    let formatObj = {
        y: string.slice(0, 4),
        m: string.slice(4, 6),
        d: string.slice(6, 8),
        h: string.slice(8, 10),
        i: string.slice(10, 12),
        s: string.slice(12, 14),
    }
    let dateStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        return formatObj[key]
    })
    return dateStr
}
function compare () {
    return function (a, b) {
        let value1 = (new Date(a)).getTime()
        let value2 = (new Date(b)).getTime()
        return value1 - value2
    }
}
